home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / pmap_prot2.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  4KB  |  126 lines

  1. /*
  2.  * $Id: pmap_prot2.c,v 1.2 1993/11/10 02:16:14 jraja Exp $
  3.  *
  4.  * $Log: pmap_prot2.c,v $
  5.  * Revision 1.2  1993/11/10  02:16:14  jraja
  6.  * Fixed includes (added the sys/param.h).
  7.  *
  8.  */
  9. /* @(#)pmap_prot2.c    2.1 88/07/29 4.0 RPCSRC */
  10. /*
  11.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12.  * unrestricted use provided that this legend is included on all tape
  13.  * media and as a part of the software program in whole or part.  Users
  14.  * may copy or modify Sun RPC without charge, but are not authorized
  15.  * to license or distribute it to anyone else except as part of a product or
  16.  * program developed by the user.
  17.  * 
  18.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21.  * 
  22.  * Sun RPC is provided with no support and without any obligation on the
  23.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  24.  * modification or enhancement.
  25.  * 
  26.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28.  * OR ANY PART THEREOF.
  29.  * 
  30.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31.  * or profits or other special, indirect and consequential damages, even if
  32.  * Sun has been advised of the possibility of such damages.
  33.  * 
  34.  * Sun Microsystems, Inc.
  35.  * 2550 Garcia Avenue
  36.  * Mountain View, California  94043
  37.  */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
  40. #endif
  41.  
  42. /*
  43.  * pmap_prot2.c
  44.  * Protocol for the local binder service, or pmap.
  45.  *
  46.  * Copyright (C) 1984, Sun Microsystems, Inc.
  47.  */
  48.  
  49. #include <sys/param.h>
  50. #include <rpc/types.h>
  51. #include <rpc/xdr.h>
  52. #include <rpc/pmap_prot.h>
  53.  
  54.  
  55. /* 
  56.  * What is going on with linked lists? (!)
  57.  * First recall the link list declaration from pmap_prot.h:
  58.  *
  59.  * struct pmaplist {
  60.  *    struct pmap pml_map;
  61.  *    struct pmaplist *pml_map;
  62.  * };
  63.  *
  64.  * Compare that declaration with a corresponding xdr declaration that 
  65.  * is (a) pointer-less, and (b) recursive:
  66.  *
  67.  * typedef union switch (bool_t) {
  68.  * 
  69.  *    case TRUE: struct {
  70.  *        struct pmap;
  71.  *         pmaplist_t foo;
  72.  *    };
  73.  *
  74.  *    case FALSE: struct {};
  75.  * } pmaplist_t;
  76.  *
  77.  * Notice that the xdr declaration has no nxt pointer while
  78.  * the C declaration has no bool_t variable.  The bool_t can be
  79.  * interpreted as ``more data follows me''; if FALSE then nothing
  80.  * follows this bool_t; if TRUE then the bool_t is followed by
  81.  * an actual struct pmap, and then (recursively) by the 
  82.  * xdr union, pamplist_t.  
  83.  *
  84.  * This could be implemented via the xdr_union primitive, though this
  85.  * would cause a one recursive call per element in the list.  Rather than do
  86.  * that we can ``unwind'' the recursion
  87.  * into a while loop and do the union arms in-place.
  88.  *
  89.  * The head of the list is what the C programmer wishes to past around
  90.  * the net, yet is the data that the pointer points to which is interesting;
  91.  * this sounds like a job for xdr_reference!
  92.  */
  93. bool_t XDRFUN
  94. xdr_pmaplist(xdrs, rp)
  95.     register XDR *xdrs;
  96.     register struct pmaplist **rp;
  97. {
  98.     /*
  99.      * more_elements is pre-computed in case the direction is
  100.      * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
  101.      * xdr_bool when the direction is XDR_DECODE.
  102.      */
  103.     bool_t more_elements;
  104.     register int freeing = (xdrs->x_op == XDR_FREE);
  105.     register struct pmaplist **next;
  106.  
  107.     while (TRUE) {
  108.         more_elements = (bool_t)(*rp != NULL);
  109.         if (! xdr_bool(xdrs, &more_elements))
  110.             return (FALSE);
  111.         if (! more_elements)
  112.             return (TRUE);  /* we are done */
  113.         /*
  114.          * the unfortunate side effect of non-recursion is that in
  115.          * the case of freeing we must remember the next object
  116.          * before we free the current object ...
  117.          */
  118.         if (freeing)
  119.             next = &((*rp)->pml_next); 
  120.         if (! xdr_reference(xdrs, (caddr_t *)rp,
  121.             (u_int)sizeof(struct pmaplist), xdr_pmap))
  122.             return (FALSE);
  123.         rp = (freeing) ? next : &((*rp)->pml_next);
  124.     }
  125. }
  126.